/**
 * Created by ND on 2014/12/26.
 */
var m;
var n;
var b;
function Game(mc, nc, mb) {
    m = mc;
    n = nc;
    b = mb;
    var game=this;

    document.body.addEventListener("click", method(this,"leftListener"));
    document.body.oncontextmenu=function(e){
        e.preventDefault();
        game.righListener(e);

    }
    this.newGame(b);
}
var squares=[];
Game.prototype.newGame = function (b) {
    var ff = dom("div");
    var buei = [m * n];
    for (var i = 0; i < m; i++) {
        var row=[];
        for (var j = 0; j < n; j++) {
            var s=new Square(new Point(i,j),i,j);
            ff.appendChild(s.img);
            row.push(s);
            document.body.appendChild(s.img);
        }
        squares.push(row);
        document.body.appendChild(dom("br"))

    }
    //开始布雷
    for (var xy = 0; xy < b; xy++) {
        var x = Math.floor(Math.random() * m);
        var y = Math.floor(Math.random() * n);
        var change = window.document.getElementById(x + "-" + y);
        if (change.panduan == "Mine") {
            xy--;
        }
        change.panduan = "Mine";

    }
    //添加数字统计雷数
    for (var i = 0; i < m; i++) {
        for (var j = 0; j < n; j++) {
            var tongji = window.document.getElementById(i + "-" + j);
            if (tongji.panduan != "Mine") {
                var count = 0;
                //循环九个方格统计雷数
                for (var ni = i - 1; ni <= i + 1; ni++) {
                    for (var nj = j - 1; nj <= j + 1; nj++) {
                        var tongjilei = window.document.getElementById(ni + "-" + nj);
                        if (tongjilei && (tongjilei.panduan == "Mine")) {
                            count++;
                        }
                    }
                }
                tongji.panduan = count;

            }

        }
    }
};
//左键点击
Game.prototype.leftListener = function (e) {

    if(e.target.zheng==0) {
        e.target.zheng = 2;
        if (e.target.panduan == 0 && e.target.dianji == false) {
             // console.log(e.target.id);
             e.target.src = "Images/Cells/" + e.target.panduan + ".png";
             var str = e.target.id;
             var x = str.substring(0,str.indexOf("-"));
             var y = str.substring(str.indexOf("-")+1);
             console.log(x);
             var square = e.target.square;
             this.saoLeiDigui(x,y);
        } else if (e.target.panduan == "Mine" && e.target.dianji == false) {
            e.target.src = "Images/Cells/" + e.target.panduan + ".png";
            alert("游戏结束！！");
            for (var i = 0; i < m; i++) {
                for (var j = 0; j < n; j++) {
                    var allOpen = window.document.getElementById(i + "-" + j);
                    allOpen.src = "Images/Cells/" + allOpen.panduan + ".png";
                }
            }
        } else {
          e.target.src = "Images/Cells/" + e.target.panduan + ".png";
        }
        e.target.dianji = true;
        if(this.won()){
           alert("You win!!!");
        }

    }

}
var leishu=b;
var flagcount=0;
Game.prototype.righListener=function(e){

    if( e.target.zheng==0){
        e.target.src="Images/Cells/NewFlag.png"
        e.target.zheng=1;
        flagcount++;
        if(e.target.panduan=="Mine"){
            leishu--;
            if(leishu==0 && flagcount==b){
                alert("You Win!!!");
            }
        }

    }
    else if(e.target.zheng==1){
        e.target.src="Images/Cells/BlueSquare.png"
        e.target.zheng=0;
        flagcount--;
        if(e.target.panduan=="Mine"){
            leishu++;
            }
        }
    }





//递归排雷
Game.prototype.saoLeiDigui = function (x,y) {

    var s= x+"-"+y;
    var square=document.getElementById(s);
    square.src = "Images/Cells/" + square.panduan + ".png";
    square.dianji = true;

        if (square.panduan == 0) {
            var l=x-(-2);
            var h=y-(-2);
            for(var p=x-1;p<l;p++) {
                for (var q = y - 1; q < h; q++) {
                    var r = p + "-" + q;
                    var oElement = document.getElementById(r);
                    if (oElement) {
                        oElement.src = "Images/Cells/" + oElement.panduan + ".png";
                        if (oElement.panduan == 0 && oElement.dianji == false) {
                            this.saoLeiDigui(p, q);
                        }
                        oElement.dianji=true;
                    }

                }
            }
        }else{
            square.src = "Images/Cells/" + square.panduan + ".png" ;

        }
}


Game.prototype.won=function(){
    var count=0;
    for(var i=0;i<9;i++){
        for(var j=0;j<9;j++){
            var r = i + "-" + j;
            var square = document.getElementById(r);
            if(!square.dianji){
                count++;
            }
            console.log(count);
        }
    }
    return count<=b;
}




